package ClientCommands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONObject;

import Annotations.IsClientCommand;
import Commands.ClientCommandBase;
import Commands.ExecuteMethod;
import DomainLayer.MessageEntity;
import Helpers.ApplicationLogger;
import Presenters.JSONPresenter;
import ServiceLayer.ServiceFacade;

@IsClientCommand
public class GetMessagesByUserId extends ClientCommandBase<List<JSONPresenter>> {

	private class MessageJSONPresenter implements JSONPresenter{

		public MessageJSONPresenter(MessageEntity message)
		{
			setId(message.getId());
			setMessage(message.getMessageString());
		}
		
		private Integer id;
		private String message;
		
		public void setId(Integer id) {
			this.id = id;
		}

		public Integer getId() {
			return id;
		}	
		
		public void setMessage(String value) {
			this.message = value;
		}

		public String getMessage() {
			return message;
		}
		
		public String toJSONString() {
			StringBuffer sb = new StringBuffer();                                  
			sb.append("{");                        
			sb.append("\"" + JSONObject.escape("Id") + "\"");                 
			sb.append(":");                 
			sb.append("\"" + JSONObject.escape(getId().toString()) + "\"");                                  
			sb.append(",");                                  
			sb.append("\"" + JSONObject.escape("Messages") + "\"");                 
			sb.append(":");                 
			sb.append("\"" + JSONObject.escape(getMessage()) + "\"");   
			sb.append("}");                                  
			return sb.toString();
		}
	}
	
	
	private String userId;
	
	public String getUserId() { return userId; }
	public void setUserId(String value) { userId = value; }
	
	public GetMessagesByUserId()
	{
		setCommandName("GetMessagesByUserId");
		setCommandDescription("Get Messages by User Id");
		setExecuteMethod(ExecuteMethod.ServerOnly);
		setUserId("-1");
	}
	
	@Override
	protected void ExecuteInternal() throws IOException 
	{
		List<JSONPresenter> messageList = new ArrayList<JSONPresenter>();
		try {
			ServiceLayer.ServiceFacade sf = new ServiceFacade();
			List<MessageEntity> messages = (List<MessageEntity>) sf.GetMessagesByUserId(getUserId());
			for (MessageEntity messageEntity : messages) {
				messageList.add(new MessageJSONPresenter(messageEntity));
			}
		} catch (Throwable e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage());
		}
        if (messageList.isEmpty())
        {
            setIsNull(true);
        }
        setResult(messageList);		
	}
	
	public void FillFromJSON(JSONObject jObj) {
		super.FillFromJSON(jObj);
		setUserId((String)jObj.get("UserId"));
	}
	
	public String toJSONString()
	{
		StringBuffer sb = new StringBuffer();                                  
		sb.append("{");                        
		sb.append("\"" + JSONObject.escape("CommandName") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + JSONObject.escape(getCommandName()) + "\"");                                  
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("CommandDescription") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + getCommandDescription() + "\"");   
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("ExecuteMethod") + "\"");    
		sb.append(":");
		sb.append("\"" + getExecuteMethod() + "\"");   
		sb.append(",");
		sb.append("\"" + JSONObject.escape("UserId") + "\"");
		sb.append(":");
		sb.append("\"" + getUserId() + "\""); 
		sb.append("}");                                  
		return sb.toString();
	}

}
